// pages/Type/TypeDes/index.js
var common = require("../../../../public.js");
Page({
  data: {
    isMore: true,
    storeList: [],
    pageNum: 1,
    pageSize: 10,
    cid: '',
    imageUrl: '',
    lat: '',
    lon: '',
    isLoadingBusy: false,
    refreshing: false,
    managerId:'',
  },
  onLoad: function (options) {
    console.log(options)
    const that = this
    const name = options.name
    if (name) {
      wx.setNavigationBarTitle({
        title: name,
      })
    }
    that.setData({
      managerId:options.managerId ? options.managerId :'',
      cid: options.id,
      imageUrl: that.selectImg(options.id),
      isLoadingBusy: true
    }, function () {
      getApp().getLocation(function (data) {
        console.log("666664444")
        console.log(data)
        that.data.lon = data.longitude
        that.data.lat = data.latitude
        that.data.city = data.regeocodeData.addressComponent.city
        // that.data.city = data.regeocodeData.addressComponent.city
        that.getStoreList(true)
      }, function () {
        // 定位失败回调
        that.setData({
          isLoadingBusy: false
        })
      })
    })
  },
  selectImg(id) {
    let imgUrl = ''
    console.log(id,"iiiiiiiiiiiiiiiiii")
    switch (id) {
      case '1': // 
        imgUrl = 'http://feiyangimage.oss-cn-shanghai.aliyuncs.com/store_cat_content/%E6%95%99%E8%82%B2%E6%B5%B7%E6%8A%A5.png'
        break;
      case '4':
        imgUrl = 'http://feiyangimage.oss-cn-shanghai.aliyuncs.com/store_cat_content/%E7%8E%89%E5%99%A8.png'
        break;
      case '7':
        imgUrl = 'http://feiyangimage.oss-cn-shanghai.aliyuncs.com/store_cat_content/%E8%AF%97%E9%85%92%E8%8A%B1%E8%8C%B6.png'
        break;
      case '5':
        imgUrl = 'http://feiyangimage.oss-cn-shanghai.aliyuncs.com/store_cat_content/%E5%91%A8%E8%BE%B9%E6%B8%B8%E6%B5%B7%E6%8A%A5.png'
        break;
      case '18':
        imgUrl = 'http://feiyangimage.oss-cn-shanghai.aliyuncs.com/ad_storeListTop/18/1646874519260845.png'
        break;  
      case '29':
          imgUrl = 'http://feiyangimage.oss-cn-shanghai.aliyuncs.com/ad_storeListTop/29/1646874448125064.png'
          break;  
      case '28':
            imgUrl = ' http://feiyangimage.oss-cn-shanghai.aliyuncs.com/ad_storeListTop/28/1646874485770416.png'
            break;  
            // 
       case '30':
              imgUrl = 'http://feiyangimage.oss-cn-shanghai.aliyuncs.com/ad_storeListTop/30/1646874576713885.png'
              break;        
      default:
        imgUrl = 'http://feiyangimage.oss-cn-shanghai.aliyuncs.com/store_cat_content/%E6%95%99%E8%82%B2%E6%B5%B7%E6%8A%A5.png'
        break;

    }
    return imgUrl
  },
  storeListClick(e) {
    const item = e.currentTarget.dataset.item
    if(item.storeType==30){  //酒店页面
      var url =  '/packageB/pages/hotel/hotel?id=' + item.id + '&name=' + item.storeName
    }else if(item.storeType==29){    //餐饮
     var url =  '/packageA/pages/activety/activety?id=' + item.id + '&name=' + item.storeName
    }else{
      var url = '/packageA/pages/Goods/storePage/index?id=' + item.id + '&name=' + item.storeName
    }
    wx.navigateTo({  
      url: url
    })
  },

  goodsImgClick(e) {
    const item = e.currentTarget.dataset.item
    common.gotoGoodsPage(item)
  },
  getStoreList(refresh = false) {
    if (!refresh && (this.data.isLoadingBusy || this.data.noMoreData)) {
      return;
    }
    this.setData({
      isLoadingBusy: true,
      refreshing: refresh
    })
    let that = this
    const pageNum = refresh ? 1 : this.data.pageNum + 1
    let tempJson ={}
    let url =''
    // 判断是否有managerId
    if(that.data.managerId){
       tempJson = {
        managerId:that.data.managerId,
        xpoint: that.data.lon,
        ypoint: that.data.lat,
        storeTypeId: this.data.cid,
        pageNum,
        pageSize: that.data.pageSize,
      }
      url = '/store/getSearchStoreListByStoreTypeIdAndMangerId'
    } else{
      tempJson = {
        storeTypeId: this.data.cid,
        pageNum,
        pageSize: that.data.pageSize,
        xpoint: that.data.lon,
        ypoint: that.data.lat,
        regionId:common.regionId,
      }
      url ='/wlplatfrom/getSearchStoreListWithCity'

    }
  
    common.post(url, tempJson, function (res) {
      if (res.status == 200) {
        that.data.pageNum = pageNum
        that.setData({
          storeList: refresh ? res.data : [...that.data.storeList, ...res.data]
        })
      } else {
        that.setData({
          noMoreData: true
        })
      }
    }, function () {
      that.setData({
        isLoadingBusy: false,
        refreshing: false
      })
    })
  },
  onReachBottom() {
    this.getStoreList(false)
  }
})